package com.eco.fanliapp.controller.app;

import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppVersion;
import com.eco.fanliapp.service.IAppVersionService;
import com.eco.fanliapp.service.RedisTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ProjectName: fanliapp
 * @Package: com.eco.fanliapp.controller.app
 * @ClassName: AppVersionAllController
 * @Author: xiaoxx
 * @Description: 版本号
 * @Date: 2019/11/28 15:13
 * @Version: 4.2.2
 */
@Controller
@RequestMapping("/mobile/v")
public class AppVersionAllController extends BaseController {
	
	private static final Logger log = LoggerFactory.getLogger(AppVersionAllController.class);

	@Autowired
	private IAppVersionService iAppVersionService;

	@Autowired
	private RedisTemplateService redisTemplateService;
	/**
	 * app版本查询-used
	 * @param platform
	 * @param version
	 * @return
	 */
	@GetMapping("/version")
	@ResponseBody
	public ServerResponse<AppVersion> findAppVersion(String platform, String version){

		String redisKeyStr = "app_version_"+ platform;
		AppVersion version2 = null;

		if (redisTemplateService.hasKey(redisKeyStr)) {
			version2 = redisTemplateService.getTByKey(redisKeyStr, AppVersion.class);
		}
		if (version2 == null) {
			version2 = iAppVersionService.selectAppVersionAllById(platform);
			if (version2 != null) {
				redisTemplateService.set(redisKeyStr, version2, 60 * 5);
			}
		}
		if (version2 != null) {
			if (version2.getVersion().equals(version)) {
				return ServerResponse.createBySuccessMessage("当前是最新版本");
			} else {
				log.info("message当前版本{}新版本{}", version, version2.getVersion());
				return ServerResponse.createBySuccess("数据获取成功", version2);
			}
		} else {
			return ServerResponse.createBySuccessCodeMessage(201, "暂无数据");
		}
	}
	
}
